﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace einuorg_console
{
    public class Terminal
    {
        public static int Width()
        {
            return 80;
        }
        public static void SetColors()
        {
            Console.BackgroundColor = ConsoleColor.Black;
            Console.ForegroundColor = ConsoleColor.White;
        }
        public static void SetColors(ConsoleColor background, ConsoleColor foreground)
        {
            Console.BackgroundColor = background;
            Console.ForegroundColor = foreground;
            Clear();
        }
        public static void Write(string text, ConsoleColor background, ConsoleColor foreground)
        {
            Console.BackgroundColor = background;
            Console.ForegroundColor = foreground;
            Console.Write(text);
            SetColors();
        }
        public static void Write(string text, ConsoleColor foreground)
        {
            Console.ForegroundColor = foreground;
            Console.Write(text);
            SetColors();
        }
        public static void Write(string text)
        {
            Console.Write(text);
            SetColors();
        }
        public static void WriteLine(string text, ConsoleColor background, ConsoleColor foreground)
        {
            Console.BackgroundColor = background;
            Console.ForegroundColor = foreground;
            Console.WriteLine(text);
            SetColors();
        }
        public static void WriteLine(string text, ConsoleColor foreground)
        {
            Console.ForegroundColor = foreground;
            Console.WriteLine(text);
            SetColors();
        }
        public static void WriteLine(string text)
        {
            Console.WriteLine(text);
            SetColors();
        }
        public static void EmptyLine()
        {
            Console.WriteLine();
        }
        public enum TabState
        {
            Info,
            Error,
            Warning,
            Success,
            Default
        }
        public enum BracketState
        {
            Info,
            Error,
            Warning,
            Success,
            Default
        }
        public static void Tab(TabState state)
        {
            switch(state)
            {
                case TabState.Error:
                    Write(" //  ", ConsoleColor.Red);
                    break;

                case TabState.Info:
                    Write(" //  ", ConsoleColor.Blue);
                    break;

                case TabState.Success:
                    Write(" //  ", ConsoleColor.Green);
                    break;

                case TabState.Warning:
                    Write(" //  ", ConsoleColor.Yellow);
                    break;

                case TabState.Default:
                    Write(" //  ", ConsoleColor.Gray);
                    break;
            }
        }
        public static void Brackets(string text, BracketState state)
        {
            switch (state)
            {
                case BracketState.Error:
                    Write("[", ConsoleColor.Red);
                    Write(text, ConsoleColor.Red);
                    Write("]  ", ConsoleColor.Red);
                    break;

                case BracketState.Info:
                    Write("[", ConsoleColor.Blue);
                    Write(text, ConsoleColor.Blue);
                    Write("]  ", ConsoleColor.Blue);
                    break;

                case BracketState.Success:
                    Write("[", ConsoleColor.Green);
                    Write(text, ConsoleColor.Green);
                    Write("]  ", ConsoleColor.Green);
                    break;

                case BracketState.Warning:
                    Write("[", ConsoleColor.Yellow);
                    Write(text, ConsoleColor.Yellow);
                    Write("]  ", ConsoleColor.Yellow);
                    break;

                case BracketState.Default:
                    Write("[", ConsoleColor.Gray);
                    Write(text, ConsoleColor.Gray);
                    Write("]  ", ConsoleColor.Gray);
                    break;
            }
        }
        public static void Clear()
        {
            Console.Clear();
        }
        public static string ReadLine()
        {
            string input = Console.ReadLine();
            Console.CursorTop -= 1;
            return input;
        }
        public static void WriteLineCenter(string text)
        {
            int TextLength = text.Length;
            int EmptySpace = (Width() - TextLength) / 2;
            string SpaceCharacter = " ";

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < EmptySpace; i++)
            {
                sb.Append(SpaceCharacter);
            }
            
            Write(sb.ToString());
            Write(text);
            if (TextLength % 2 == 0)
            {
                WriteLine(sb.ToString());
            }
            else
            {
                WriteLine(sb.ToString() + SpaceCharacter);
            }
        }
        public static void WriteLineCenter(string text, string spaceChar)
        {
            int TextLength = text.Length;
            int EmptySpace = (Width() - TextLength) / 2;
            string SpaceCharacter = spaceChar;

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < EmptySpace; i++)
            {
                sb.Append(SpaceCharacter);
            }

            Write(sb.ToString());
            Write(text);
            if (TextLength % 2 == 0)
            {
                WriteLine(sb.ToString());
            }
            else
            {
                WriteLine(sb.ToString() + SpaceCharacter);
            }
        }
        public static void WriteLineCenter(string text, string spaceChar, ConsoleColor foreground)
        {
            int TextLength = text.Length;
            int EmptySpace = (Width() - TextLength) / 2;
            string SpaceCharacter = spaceChar;

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < EmptySpace; i++)
            {
                sb.Append(SpaceCharacter);
            }

            Write(sb.ToString());
            Write(text, foreground);
            if (TextLength % 2 == 0)
            {
                WriteLine(sb.ToString());
            }
            else
            {
                WriteLine(sb.ToString() + SpaceCharacter);
            }
        }
        public static void WriteLineCenter(string text, string spaceChar, ConsoleColor foreground, ConsoleColor background)
        {
            int TextLength = text.Length;
            int EmptySpace = (Width() - TextLength) / 2;
            string SpaceCharacter = spaceChar;

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < EmptySpace; i++)
            {
                sb.Append(SpaceCharacter);
            }

            Write(sb.ToString());
            Write(text, background, foreground);
            if (TextLength % 2 == 0)
            {
                WriteLine(sb.ToString());
            }
            else
            {
                WriteLine(sb.ToString() + SpaceCharacter);
            }
        }
        public enum SeparatorState
        {
            Big,
            Small,
            SmallCenter
        }
        public static void Separator(SeparatorState state)
        {
            string SeparatorChar = "_";

            StringBuilder bs = new StringBuilder();
            for (int i = 0; i < Width(); i++)
            {
                bs.Append(SeparatorChar);
            }
            string BigSeparator = bs.ToString();

            StringBuilder ss = new StringBuilder();
            for (int i = 0; i < (Width() / 2); i++ )
            {
                ss.Append(SeparatorChar);
            }
            string SmallSeparator = ss.ToString();
            
                switch (state)
                {
                    case SeparatorState.Big:
                        WriteLine(BigSeparator);
                        break;

                    case SeparatorState.Small:
                        WriteLine(SmallSeparator);
                        break;

                    case SeparatorState.SmallCenter:
                        WriteLineCenter(SmallSeparator);
                        break;
                }
        }
        public static void Separator(SeparatorState state, ConsoleColor color)
        {
            string SeparatorChar = "_";

            StringBuilder bs = new StringBuilder();
            for (int i = 0; i < Width(); i++)
            {
                bs.Append(SeparatorChar);
            }
            string BigSeparator = bs.ToString();

            StringBuilder ss = new StringBuilder();
            for (int i = 0; i < (Width() / 2); i++)
            {
                ss.Append(SeparatorChar);
            }
            string SmallSeparator = ss.ToString();

            switch (state)
            {
                case SeparatorState.Big:
                    WriteLine(BigSeparator, color);
                    break;

                case SeparatorState.Small:
                    WriteLine(SmallSeparator, color);
                    break;

                case SeparatorState.SmallCenter:
                    WriteLineCenter(SmallSeparator, " ", color);
                    break;
            }
        }
    }
}